package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.ts.api.http.org.entity.apply.OrgApplyLongYanQiNuoShangHai3;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import static com.gitee.apanlh.web.http.HttpContentType.APPLICATION_JSON;

/**
 * 安心分期-龙岩市企诺API全量15 上海3
 *
 * @author 李源聪
 */
@ApiPlan(productId = "348", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5LongYanQiNuoShangHai3 implements ApiOrgPhoneMd5ConvertHandler {

    private static final String BRAND = "1571";

    private static final String SOURCE = "lyzd-rkt";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        apiDataExchangeParameter.setHttpContentType(APPLICATION_JSON);
        return MapUtils.newHashMap(newMap -> {
            newMap.put("md5mobile", apiDataExchangeParameter.getPhoneMd5());
            newMap.put("brand", BRAND);
        });
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("data"), 1)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        apiApplyParameter.setHttpContentType(APPLICATION_JSON);
        OrgApplyLongYanQiNuoShangHai3 data = new OrgApplyLongYanQiNuoShangHai3();
        // 客户姓名
        data.setName(apiApplyParameter.getUserName());
        data.setMobile(apiApplyParameter.getPhone());
        // 城市(不包含市，比如“杭州市”应该是“杭州”)
        data.setCity(apiApplyParameter.getCity().replace("市", ""));
        // 是 否 有 车 (“ 有 ” 或“无”)
        String car = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", "无")
                .when(apiApplyParameter.getCarInfo(), "2", "有")
                .when(apiApplyParameter.getCarInfo(), "3", "有")
                .when(apiApplyParameter.getCarInfo(), "4", "有")
                .end("无");
        data.setCar(car);
        // 年龄(如：39)
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        data.setAge(apiFieldConverter.getAgeInt());
        // 工作(“有”或“无”)
        String career = ChooseEq.create()
                .when(apiApplyParameter.getCareer(), "1", "有")
                .when(apiApplyParameter.getCareer(), "2", "无")
                .when(apiApplyParameter.getCareer(), "3", "有")
                .when(apiApplyParameter.getCareer(), "4", "有")
                .when(apiApplyParameter.getCareer(), "5", "有")
                .end("无");
        data.setJob(career);
        // 房(“有”或“无”)
        String realEstateInfo = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", "无")
                .when(apiApplyParameter.getRealEstateInfo(), "2", "有")
                .when(apiApplyParameter.getRealEstateInfo(), "3", "有")
                .when(apiApplyParameter.getRealEstateInfo(), "4", "有")
                .when(apiApplyParameter.getRealEstateInfo(), "5", "有")
                .when(apiApplyParameter.getRealEstateInfo(), "6", "有")
                .end("无");
        data.setHouse(realEstateInfo);
        // 保单(“有”或“无”)
        String insurancePolicy = ChooseEq.create()
                .when(apiApplyParameter.getInsurancePolicy(), "1", "无")
                .when(apiApplyParameter.getInsurancePolicy(), "2", "有")
                .when(apiApplyParameter.getInsurancePolicy(), "3", "有")
                .when(apiApplyParameter.getInsurancePolicy(), "4", "有")
                .end("无");
        data.setBaodanIs(insurancePolicy);
        // 性别(“男”或“女”)
        String sex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "0", "男")
                .when(apiApplyParameter.getSex(), "1", "女")
                .end("男");
        data.setSex(sex);
        // 贷款额度(如：20，即单位为万)
        Integer money = ChooseEq.create()
                .when(apiApplyParameter.getLoanAmount(), "1", 30)
                .when(apiApplyParameter.getLoanAmount(), "2", 50)
                .when(apiApplyParameter.getLoanAmount(), "3", 100)
                .when(apiApplyParameter.getLoanAmount(), "4", 200)
                .end(300);
        data.setMoney(money);
        // 贷款来源
        data.setSource(SOURCE);
        // 贷款来源特别说明
        data.setSourceRemark("实时申请：腾讯百度朋友圈小红书");
        // 社保(“有”或“无”)
        String shebao = ChooseEq.create()
                .when(apiApplyParameter.getSocialSecurity(), "1", "无")
                .when(apiApplyParameter.getSocialSecurity(), "2", "有")
                .when(apiApplyParameter.getSocialSecurity(), "3", "有")
                .when(apiApplyParameter.getSocialSecurity(), "4", "有")
                .end("无");
        data.setShebao(shebao);
        // 公积金(“有”或“无”)
        String providentFund = ChooseEq.create()
                .when(apiApplyParameter.getProvidentFund(), "1", "无")
                .when(apiApplyParameter.getProvidentFund(), "2", "有")
                .when(apiApplyParameter.getProvidentFund(), "3", "有")
                .when(apiApplyParameter.getProvidentFund(), "4", "有")
                .end("无");
        data.setGongjijin(providentFund);
        // 信用卡(“有”或“无”)
        String creditCard = ChooseEq.create()
                .when(apiApplyParameter.getCreditLimit(), "2", "有")
                .when(apiApplyParameter.getCreditLimit(), "3", "有")
                .when(apiApplyParameter.getCreditLimit(), "4", "有")
                .when(apiApplyParameter.getCreditLimit(), "5", "有")
                .when(apiApplyParameter.getCreditLimit(), "1", "无")
                .when(apiApplyParameter.getCreditLimit(), "6", "有")
                .end("无");
        data.setCreditCard(creditCard);
        //是否有企业(“有”或“无”)
        String company = ChooseEq.create()
                .when(apiApplyParameter.getBizLicenseYear(), "1", "无")
                .when(apiApplyParameter.getBizLicenseYear(), "2", "有")
                .when(apiApplyParameter.getBizLicenseYear(), "3", "有")
                .when(apiApplyParameter.getBizLicenseYear(), "4", "有")
                .when(apiApplyParameter.getBizLicenseYear(), "5", "有")
                .end("无");
        data.setCompany(company);
        // 芝麻分(整数,如:567)
        data.setZhima(apiFieldConverter.getSesameCreditText());
        // 银行代发(“银行代发”或“无”)
        String isBankPay = ChooseEq.create()
                .when(apiApplyParameter.getSalaryPayMethod(), "1", "银行代发")
                .when(apiApplyParameter.getSalaryPayMethod(), "2", "无")
                .when(apiApplyParameter.getSalaryPayMethod(), "3", "无")
                .end("无");
        data.setIsbankpay(isBankPay);
        // 该条数据实际申请时间戳（精确到秒）， 如：1467100609）
        data.setTime(System.currentTimeMillis() / 1000);
        return JsonUtils.toJson(data);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
